use std::collections::HashMap;
use std::result::Result;
use crate::db_core::DBError::NotExisted;

pub enum DBError {
    /// success finish options
    #[stable(feature = "rust1", since = "1.0.0")]
    Success,

    /// key not existed in db
    #[stable(feature = "rust1", since = "1.0.0")]
    NotExisted,
}


pub trait DBCore {
    fn get(&self, k: &str) -> std::result::Result<&str, DBError>;
    fn set(&self, k: &str, v: &str) -> std::result::Result<&str, DBError>;
    fn delete(&self, k: &str) -> std::result::Result<&str, DBError>;
    fn scan(&self, reg_k: &str) -> std::result::Result<Vec<&str>, DBError>;
}


pub struct TestDBCore {
    map: HashMap<str, str>
}

impl DBCore for TestDBCore {
    fn get(&self, k: &str) -> std::result::Result<&str, DBError> {
        let r = self.map.get(&k);
        if r != None {
            return Ok(*r)
        }
        return Err(NotExisted)
    }

    fn set(&mut self, k: &str, v: &str) -> Result<&str, DBError> {
        self.map.insert(**k, **v);
        return Ok("")
    }

    fn delete(&mut self, k: &str) -> Result<&str, DBError> {
        self.map.remove(k);
        return Ok("")
    }

    fn scan(&self, reg_k: &str) -> Result<Vec<&str>, DBError> {
        unimplemented!()
    }
}